<?php
namespace App\Service\ApiXcx;

use App\Service\BaseService as Service;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class BaseService extends Service
{
    //基础服务

    //通过手机号获取uid
    public function getUidByPhone($phone)
    {
        if (!$phone) {
            return 0;
        }
        $res = (array) DB::table($this->dbUser)->where('phone', $phone)->first();
        if (empty($res)) {
            return 0;
        }
        return $res['id'];
    }

    //通过token获取uid
    public function getUidByToken($token)
    {
        if (!$token) {
            return 0;
        }
        $res = (array) DB::table($this->dbUser)
            ->where('token', $token)
//            ->orderBy('id', 'desc')
            ->first();
        if (empty($res)) {
            return 0;
        }
        return $res['id'];
    }

    //通过token获取用户信息
    public function getUserByToken($token)
    {
        if (!$token) {
            return 0;
        }
        $res = (array) DB::table($this->dbUser)
            ->where('token', $token)
//            ->orderBy('id', 'desc')
            ->first();
        if (empty($res)) {
            return false;
        }
        return $res;
    }

    /**
     * 插入接口统计
     * @param string $phone
     */
    public function addApiCount($token)
    {
        $uid = $this->getUidByToken($token);
        $uri = $_SERVER['REQUEST_URI'];
        $resOne = (array) DB::table($this->dbDoc)->where('uri', $uri)->first();
        if (!empty($resOne)) {
            DB::table($this->dbDoc)->where('id', $resOne['id'])
                ->update(['count' => $resOne['count'] + 1]);
            DB::table($this->dbDocCount)->insertGetId([
                'uid' => $uid,
                'doc_id' => $resOne['id'],
                'addtime' => toDate(),
            ]);
        }
        return $uid;
    }

    //获取配置
    public function getConfig($key = '')
    {
        $db = $this->dbwbConn($this->dbQdConfig);
        $res = $db->get();
        if (!$key) {
            $arys = [];
            foreach ($res as $v) {
                $v = (array) $v;
                $arys[$v['key']] = $v['value'];
            }
            return $this->toback(0, '', $arys);
        }
        $res = (array) $db->where('key', $key)->first();
        if (empty($res)) {
            return $this->toback(-2, '没有参数');
        }
        return $this->toback(0, '', $res['value']);
    }

//    /**
//     * 通过UID，获取折扣率
//     * 准备废弃1119
//     */
//    public function discountByUid($uid)
//    {
//        $uOne = (array) $this->dbConn($this->dbUser)
//            ->where('id', $uid)
//            ->first();
//        if (empty($uOne) || $uOne['is_auto'] != 3) {
//            return 1;
//        }
//        $levelOne = (array) $this->dbConn($this->dbUlevel)
//            ->where('id', $uOne['level_id'])
//            ->first();
//        if (empty($levelOne)) {
//            return 1;
//        }
//        if ($levelOne['type'] == 1) {
//            return $levelOne['discount'] / 100;
//        }
//        return $uOne['discount'] ? $uOne['discount'] / 100 : 1;
//    }

    //通过UID，获取折扣率
    public function discountByUid($uid)
    {
        $uOne = (array) $this->dbConn($this->dbUser)
            ->where('id', $uid)
            ->first();
        if (empty($uOne)) {
            return 1;
        }
        if ($uOne['is_auto'] == 3) { //3是SVIP折扣率
            return $uOne['discount'] ? $uOne['discount'] / 100 : 1;
        } else if (in_array($uOne['is_auto'], [1, 2])) { //普通等级手动|自动折扣率
            $levelOne = (array) $this->dbConn($this->dbUlevel)
                ->where('id', $uOne['level_id'])
                ->first();
            if (empty($levelOne)) {
                return 1;
            }
            if ($levelOne['type'] == 1) {
                return $levelOne['discount'] / 100;
            }
        } else { //普通或自动会员
            return 1;
        }
    }

    /**
     * 限时折扣
     * @param int $gid 商品ID
     */
    public function cheapByGid($gid)
    {
        $today = toDay();
        $res = (array) $this->dbConn($this->dbCheap)
            ->where('del', 0)
            ->where('gid', $gid)
            ->where('start_time', '<=', $today)
            ->where('end_time', '>=', $today)
            ->orderBy('id', 'desc')
            ->first();
        if (empty($res)) {
            return 1;
        }
        return $res['per'] / 100;
    }
}